
/**
 * @description: 事件总线
 * @param {*}
 * @return {*}
 */
class BusUtils {

    constructor() {
        this.callbackList = new WeakMap();
    }

    $on(type, callback) {
        if (this.callbackList[type]) {
            this.callbackList[type].push(callback);
        } else {
            this.callbackList[type] = [callback];
        }
    }

     $emit(type, data) {
        if (this.callbackList[type] && this.callbackList[type].length > 0) {
            this.callbackList[type].forEach((callback) => {
                callback(data);
            })
        }
    }

     $off(eventName) {
        if (eventName) {
            return delete this.callbackList[eventName];
        }
        this.callbackList = new WeakMap();
    }
}

module.exports = BusUtils;